use crate::fs;
use std::io;

pub struct Config {
    pub addr: String,
}

pub struct Client {
    cfg: Config,
}

impl Client {
    pub fn new(cfg: Config) -> Client {
        Client { cfg: cfg }
    }
}

impl fs::FS for Client {
    fn read(&self, name: &str) -> Result<String, io::Error> {
        println!("ufile.addr: {}, name:{:?}", self.cfg.addr, name);
        let buf = String::from("ufile content");
        Ok(buf)
    }

    fn read_to_writer(&self, name: &str, writer: &mut dyn io::Write) -> io::Result<()> {
        println!("name:{}", name);

        let mut content = name.clone().as_bytes();
        io::copy(&mut content, writer)?;
        Ok(())
    }

    fn write(&self, name: &str, content: &str) -> io::Result<()> {
        println!("name:{}, content:{}", name, content);
        Ok(())
    }

    fn write_from_reader(&self, name: &str, r: &mut dyn io::Read) -> io::Result<()> {
        let mut buf = String::new();
        r.read_to_string(&mut buf)?;
        println!("write_from_reader, name:{}, content:{:?}", name, buf);
        Ok(())
    }

    fn delete(&self, name: &str) -> io::Result<()> {
        println!("name:{}", name);
        Ok(())
    }
}

impl From<fs::Type> for Client {
    fn from(cfg: fs::Type) -> Self {
        let cfg = match cfg {
            fs::Type::Local(_) => {
                panic!("config invalid");
            }
            fs::Type::Ufile(cfg) => cfg,
        };
        Client { cfg: cfg }
    }
}
